package com.gjintel.business.proj.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.alibaba.druid.sql.visitor.functions.If;
import com.gjintel.business.proj.dao.IProjectInfoDao;
import com.gjintel.business.proj.model.ProjectBidInfo;
import com.gjintel.business.proj.model.ProjectInfo;
import com.gjintel.framework.constant.ProjectStatus;
import com.gjintel.sdk.base.dao.HibernateGenericDAO;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;
import com.gjintel.sdk.util.StringUtil;

/**
 * @description
 * @author wwang
 * @date 2016-8-16
 */
@Repository
@SuppressWarnings("unchecked")
public class ProjectInfoDaoImpl extends HibernateGenericDAO implements IProjectInfoDao {

	@Override
	public List<ProjectInfo> findAllByIds(String ids, ProjectInfo project, Page page, Sort sort, Date startTime,
			Date stopTime, Date bidStartTime, Date bidStopTime, Double startMoney, Double endMoney) {
		if (sort != null && StringUtil.isEmpty(sort.getFields())) {
			sort.setFields("createTime");
			sort.setOrders("desc");
		}
		StringBuilder hql = new StringBuilder("from ProjectInfo project where 1=1 ");
		if (ids != null) {
			if (StringUtil.isEmpty(ids)) {
				hql.append(" and project.id in('')");
			} else {
				hql.append(" and project.id in(" + ids + ")");
			}
		}
		List<Object> paramList = new ArrayList<Object>();

		// 业务数据条件
		if (project != null) {
			if (!StringUtil.isEmpty(project.getName())) {
				hql.append(" and project.name like ?");
				paramList.add("%" + project.getName() + "%");
			}
			if (!StringUtil.isEmpty(project.getCode())) {
				hql.append(" and project.code like ?");
				paramList.add("%" + project.getCode() + "%");
			}
			if (!StringUtil.isEmpty(project.getCatagoryId())) {
				hql.append(" and project.catagoryId = ?");
				paramList.add(project.getCatagoryId());
			}
			if (project.getStatus() != null) {
				hql.append(" and project.status = ?");
				paramList.add(project.getStatus());
			}

			// 查询是否退还保证金已经结束
			if (project.getReturnDepositFlag() == true) {
				hql.append(" and project.returnDepositFlag = ?");
				paramList.add(project.getReturnDepositFlag());
			}

			// 确定查询参数 在结束 时间以内
			if (project.getEndTime() != null) {
				hql.append(" and (project.endTime >= ? or project.endTime is null)");
				paramList.add(project.getEndTime());
			}

			if(!StringUtil.isEmpty(project.getExecutePromissFilter())){
				hql.append(" and project.statusExecutePromiss = ? ");
				paramList.add(project.getStatusExecutePromiss());
			}
			if(!StringUtil.isEmpty(project.getExecutePromissFilter())){
				hql.append(" and project.returnDepositFlag = ? ");
				paramList.add(project.getReturnDepositFlag());
			}
			//是否评标
			if (project.getAuditable() != null) {
				hql.append(" and  project.auditable=?");
				paramList.add(project.getAuditable());
			}
		}

		if (startTime != null) {
			hql.append(" and project.createTime >= ? ");
			paramList.add(startTime);
		}
		if (stopTime != null) {
			hql.append(" and project.createTime <= ? ");
			paramList.add(stopTime);
		}

		if (bidStartTime != null) {
			hql.append(" and project.bidStartTime >= ? ");
			paramList.add(bidStartTime);
		}
		if (bidStopTime != null) {
			hql.append(" and project.bidStartTime <= ? ");
			paramList.add(bidStopTime);
		}

		// if(startMoney != null){
		// hql.append(" and project.budgetMoney >= ? ");
		// paramList.add(startMoney);
		// }
		// if(endMoney != null){
		// hql.append(" and project.budgetMoney <= ? ");
		// paramList.add(endMoney);
		// }
		return this.findAsPage(hql.toString(), page, sort, "project", true, paramList.toArray());
	}

	@Override
	public List<ProjectInfo> findBiddList(ProjectInfo project, Page page, Sort sort, java.sql.Date startTime,
			java.sql.Date stopTime, java.sql.Date bidStartTime, java.sql.Date bidStopTime) {
		if (sort != null && StringUtil.isEmpty(sort.getFields())) {
			sort.setFields("createTime");
			sort.setOrders("desc");
		}
		// 投标人 列表 status
		// TODO ：此处hardCode，需要整改
		StringBuilder hql = new StringBuilder(
				"from ProjectInfo project where project.status in(" + getProjectStatusList() + ") ");

		List<Object> paramList = new ArrayList<Object>();

		// 业务数据条件
		if (project != null) {
			if (!StringUtil.isEmpty(project.getName())) {
				hql.append(" and project.name like ?");
				paramList.add("%" + project.getName() + "%");
			}
			if (!StringUtil.isEmpty(project.getCode())) {
				hql.append(" and project.code like ?");
				paramList.add("%" + project.getCode() + "%");
			}
			if (!StringUtil.isEmpty(project.getCatagoryId())) {
				hql.append(" and project.catagoryId = ?");
				paramList.add(project.getCatagoryId());
			}
			if (project.getStatus() != null) {
				hql.append(" and project.status = ?");
				paramList.add(project.getStatus());
			}
		}

		if (startTime != null) {
			hql.append(" and project.createTime >= ? ");
			paramList.add(startTime);
		}
		if (stopTime != null) {
			hql.append(" and project.createTime <= ? ");
			paramList.add(stopTime);
		}

		if (bidStartTime != null) {
			hql.append(" and project.bidStartTime >= ? ");
			paramList.add(startTime);
		}
		if (bidStopTime != null) {
			hql.append(" and project.bidStartTime <= ? ");
			paramList.add(stopTime);
		}
		return this.findAsPage(hql.toString(), page, sort, "project", true, paramList.toArray());
	}

	/**
	 * 待开标之后的状态
	 * 
	 * @return
	 */
	private String getProjectStatusList() {
		return ProjectStatus.WAIT_SHOT.toString() + "," + ProjectStatus.SHOTING.toString() + ","
				+ ProjectStatus.AUDITED_BEGIN.toString() + "," + ProjectStatus.AUDITED_END.toString() + ","
				+ ProjectStatus.WAIT_WIN_SURE.toString() + "," + ProjectStatus.WAIT_FAIL_SURE.toString() + ","
				+ ProjectStatus.SHOWING.toString() + "," + ProjectStatus.SHOWING_FLOW.toString() + ","
				+ ProjectStatus.SHOW_OVER.toString() + "," + ProjectStatus.SHOWING_FLOW.toString() + ","
				+ ProjectStatus.ALERT.toString() + "," + ProjectStatus.WAIT_CONTACT_AUDIT.toString() + ","
				+ ProjectStatus.CONTACT_AUDIT_NOT_PASS.toString() + "," + ProjectStatus.SHOW_CONTACT.toString() + ","
				+ ProjectStatus.SHOW_CONTACT_OVER.toString() + "," + ProjectStatus.WAIT_RECEIVING.toString() + ","
				+ ProjectStatus.RECEIVING_UNAUTH.toString() + "," + ProjectStatus.RECEIVING_SHOWING.toString() + ","
				+ ProjectStatus.WAIT_PROJ_OVER_SURE.toString() + "," + ProjectStatus.PROJ_OVER.toString() + ","
				+ ProjectStatus.FLOW_DOWN.toString() + "," + ProjectStatus.CANCEL.toString() + ","
				+ ProjectStatus.COMPAT_SHOW_PAUSE.toString() + "," + ProjectStatus.HIT_SHOW_PAUSE.toString() + ","
				+ ProjectStatus.COMPAT_SHOW_PAUSE.toString() + "," + ProjectStatus.WAIT_WIN_CHOICE.toString() + ","
				+ ProjectStatus.STATUS_LOCK.toString();
	}

	@Override
	public int getProjectBidderByProjId(String projId, boolean isEvalution) {
		String hql = "from ProjectBidInfo pb where pb.projectId=? and pb.flag=1 ";
		if (isEvalution) {// 是否查询为有效标
			hql += " and pb.evalutionResult!=2";
		}
		return this.getHQLCount(hql, projId);
	}

	@Override
	public List<ProjectBidInfo> findProjectBidList(String projectId) {
		return this.find(
				"from ProjectBidInfo pb where pb.flag=1 and pb.evalutionResult=1 and pb.projectId=? order by pb.bidTime,pb.millisecond",
				projectId);
	}

	@Override
	public ProjectBidInfo getProjectBidderByProjIdAndBidderId(String projectId, String bidderId) {
		return (ProjectBidInfo) this.findUnique("from ProjectBidInfo pb where pb.projectId=? and pb.userId=?",
				projectId, bidderId);
	}

	@Override
	public ProjectBidInfo isBidByProjIdAndBidderId(String projectId, String bidderId) {
		return (ProjectBidInfo) this.findUnique(
				"from ProjectBidInfo pb where pb.projectId=? and pb.userId=? and pb.flag=1", projectId, bidderId);
	}

	@Override
	public List<ProjectInfo> findProjectBiddList(ProjectInfo project, Page page, Sort sort, java.sql.Date startTime,
			java.sql.Date stopTime, java.sql.Date bidStartTime, java.sql.Date bidStopTime) {
		if (StringUtil.isEmpty(sort.getFields())) {
			sort.setFields("createTime");
			sort.setOrders("desc");
		}
		// 投标人 列表 status
		StringBuilder hql = new StringBuilder("from ProjectInfo project where project.status in(51) ");

		List<Object> paramList = new ArrayList<Object>();

		// 业务数据条件
		if (project != null) {
			if (!StringUtil.isEmpty(project.getName())) {
				hql.append(" and project.name like ?");
				paramList.add("%" + project.getName() + "%");
			}
			if (!StringUtil.isEmpty(project.getCode())) {
				hql.append(" and project.code like ?");
				paramList.add("%" + project.getCode() + "%");
			}
			if (!StringUtil.isEmpty(project.getCatagoryId())) {
				hql.append(" and project.catagoryId = ?");
				paramList.add(project.getCatagoryId());
			}
			if (project.getStatus() != null) {
				hql.append(" and project.status = ?");
				paramList.add(project.getStatus());
			}
		}

		if (startTime != null) {
			hql.append(" and project.createTime >= ? ");
			paramList.add(startTime);
		}
		if (stopTime != null) {
			hql.append(" and project.createTime <= ? ");
			paramList.add(stopTime);
		}

		if (bidStartTime != null) {
			hql.append(" and project.bidStartTime >= ? ");
			paramList.add(startTime);
		}
		if (bidStopTime != null) {
			hql.append(" and project.bidStartTime <= ? ");
			paramList.add(stopTime);
		}
		return this.findAsPage(hql.toString(), page, sort, "project", true, paramList.toArray());
	}

	@Override
	public List<ProjectBidInfo> findProjectBidListByProjectId(Page page, ProjectBidInfo bidInfo) {
		StringBuilder hql = new StringBuilder("from ProjectBidInfo pb where pb.flag=1 and pb.projectId=?");
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(bidInfo.getProjectId());

		if (bidInfo.getUserId() != null) {
			hql.append(" and pb.userId=?");
			paramList.add(bidInfo.getUserId());
		}

		hql.append(" order by pb.bidAmount, pb.bidTime,pb.millisecond");
		return this.findAsPage(hql.toString(), page, null, "project", true, paramList.toArray());
	}

	@Override
	public Object queryMaxCountOfSameArea(int years, String unitId, String projectType, String tradeType) {
		// String hql = "from ProjectMaxCount where unitId=? and years=? and
		// projectType=? and tradeType=?";
		String hql = "from ProjectMaxCount where unitId=? and years=? and tradeType=?";
		return this.findUnique(hql, new Object[] { unitId, years, tradeType });
	}

	@Override
	public ProjectInfo findSingleByProcessId(String processId) {
		String hql = "from ProjectInfo where workflowId=?";
		return (ProjectInfo) this.findUnique(hql, processId);
	}

	@Override
	public List<ProjectInfo> findAllByStatus(Page page, ProjectInfo pj, String status) {
		// 投标人 列表 status
		StringBuilder hql = new StringBuilder("from ProjectInfo project where project.status in(" + status + ") ");
		List<Object> paramList = new ArrayList<Object>();
		return this.findAsPage(hql.toString(), page, null, "project", true, paramList.toArray());
	}

	@Override
	public List<ProjectBidInfo> getProjectBiddersByProjId(String id) {
		String hql="from ProjectBidInfo pb where pb.flag=1 and pb.evalutionResult=1 and pb.isMultHit=true and pb.projectId=? ";
		return this.find(hql,id);
	}

}
